package com.yunhuajinfu.mian;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.support.v4.content.ContextCompat;
import android.view.KeyEvent;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.yunhuajinfu.R;
import com.yunhuajinfu.activity.LoginActivity;
import com.yunhuajinfu.activity.WebActivity;
import com.yunhuajinfu.base.BaseActivity;
import com.yunhuajinfu.fragment.ConfessFragment;
import com.yunhuajinfu.fragment.FinancialFragment;
import com.yunhuajinfu.fragment.HomeFragment;
import com.yunhuajinfu.fragment.MyCenterFragment;
import com.yunhuajinfu.tool.AbActivityManager;
import com.yunhuajinfu.util.AbToastUtil;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;
import yicheng.android.ui.materialdesignlibrary.views.LayoutRipple;

public class MainActivity extends BaseActivity {
    @Bind(R.id.tab_home_icon)
    ImageView mTabHomeIcon;
    @Bind(R.id.tab_home_text)
    TextView mTabHomeText;
    @Bind(R.id.tab_home)
    LayoutRipple mTabHome;
    @Bind(R.id.tab_financial_icon)
    ImageView mTabFinancialIcon;
    @Bind(R.id.tab_financial_text)
    TextView mTabFinancialText;
    @Bind(R.id.tab_financial)
    LayoutRipple mTabFinancial;
    @Bind(R.id.tab_my_icon)
    ImageView mTabMyIcon;
    @Bind(R.id.tab_my_text)
    TextView mTabMyText;
    @Bind(R.id.tab_my)
    LayoutRipple mTabMy;
    @Bind(R.id.tab_more_icon)
    ImageView mTabMoreIcon;
    @Bind(R.id.tab_more_text)
    TextView mTabMoreText;
    @Bind(R.id.tab_more)
    LayoutRipple mTabMore;

    //再按一次退出程序
    private long exitTime = 0;

    private FragmentManager fragmentManager;
    //Fragment事物   进行添加,移除,替换,以及执行其他动作。
    private FragmentTransaction transaction;

    //首页推荐 HomeFragment
    private HomeFragment homeFragment;
    //投资 FinancialFragment
    private FinancialFragment financialFragment;
    //认筹 ConfessFragment
    private ConfessFragment myFragment;
    // 我的 MyCenterFragment
    private MyCenterFragment moreFragment;
    //
    private  MyBroadcastReceiver mMyBroadcastReceiver;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        setContentView(R.layout.activity_main);
        translucentStatusBar();
        ButterKnife.bind(this);

        //订阅广播
        IntentFilter intentFilter=new IntentFilter();
        intentFilter.addAction(WebActivity.ACTION);
        mMyBroadcastReceiver=new MyBroadcastReceiver();
        registerReceiver(mMyBroadcastReceiver,intentFilter);
        super.onCreate(savedInstanceState);

    }

class  MyBroadcastReceiver extends BroadcastReceiver{


    @Override
    public void onReceive(Context context, Intent intent) {

//        Toast.makeText(MainActivity.this, "回调成功啦", Toast.LENGTH_SHORT).show();
        if(null!=moreFragment){
            moreFragment.updata();
        }
    }

}


    @Override
    protected void onDestroy() {
        super.onDestroy();
        if(null!=mMyBroadcastReceiver){
            unregisterReceiver(mMyBroadcastReceiver);
        }


    }

    @Override
    protected void initTitle() {

    }

    @Override
    protected void initView() {


        checkVersion();

    }

    /**
     * 检查版本跟新
     */
    private void checkVersion() {


    }

    @Override
    protected void setData() {
        fragmentManager = getSupportFragmentManager();
        switchTab(0);


    }

    @OnClick({R.id.tab_home, R.id.tab_financial, R.id.tab_my, R.id.tab_more})
    public void widgetClick(View view) {
        switch (view.getId()) {
            case R.id.tab_home:
                switchTab(0);
                break;
            case R.id.tab_financial:
                switchTab(1);
                break;
            case R.id.tab_my:
                switchTab(2);
                break;
            case R.id.tab_more:

                //要判断是否登录
                if (mpreferencesUtils.isLogin(MainActivity.this, "userAccount")) {
                    switchTab(3);
                    break;
                } else {
                    Intent intent = new Intent(MainActivity.this, LoginActivity.class);
                    //表示这是直接点击我的页面进行是否登录判断
                    intent.putExtra("flag", 100);
                    startActivityForResult(intent, 100);

                }


                break;
        }
    }

    public void switchTab(int index) {
        initTab();
        transaction = fragmentManager.beginTransaction();
        hideFragment(transaction);
        switch (index) {
            case 0:
                if (homeFragment == null) {
                    homeFragment = new HomeFragment();
                    transaction.add(R.id.container, homeFragment);
                } else {
                    transaction.show(homeFragment);
                    //第一个fragment需要及时更新数据
//                    homeFragment.update();
                }
                mTabHomeIcon.setImageResource(R.mipmap.tuijian);
                mTabHomeText.setTextColor(ContextCompat.getColor(context, R.color.bule));
                break;
            case 1:
                if (financialFragment == null) {
                    financialFragment = new FinancialFragment();
                    transaction.add(R.id.container, financialFragment);
                } else {
                    transaction.show(financialFragment);
                }
                mTabFinancialIcon.setImageResource(R.mipmap.touzi);
                mTabFinancialText.setTextColor(ContextCompat.getColor(context, R.color.bule));
                break;
            case 2:
                if (myFragment == null) {
                    myFragment = new ConfessFragment();
                    transaction.add(R.id.container, myFragment);
                } else {
                    transaction.show(myFragment);
                }

                mTabMyIcon.setImageResource(R.mipmap.renchou);
                mTabMyText.setTextColor(ContextCompat.getColor(context, R.color.bule));
                break;
            case 3:
                if (moreFragment == null) {
                    moreFragment = new MyCenterFragment();
                    transaction.add(R.id.container, moreFragment);
                } else {
                    // 如果indexFragment不为空，则直接将它显示出来
                    transaction.show(moreFragment);
                }
                mTabMoreIcon.setImageResource(R.mipmap.mycenter);
                mTabMoreText.setTextColor(ContextCompat.getColor(context, R.color.bule));
                break;

        }

        transaction.commitAllowingStateLoss();
    }

    private void initTab() {
        mTabHomeIcon.setImageResource(R.mipmap.tuijian_1);
        mTabFinancialIcon.setImageResource(R.mipmap.touzi_1);
        mTabMyIcon.setImageResource(R.mipmap.renchou_1);
        mTabMoreIcon.setImageResource(R.mipmap.mycenter_1);

        mTabHomeText.setTextColor(ContextCompat.getColor(context, R.color.tab_text));
        mTabFinancialText.setTextColor(ContextCompat.getColor(context, R.color.tab_text));
        mTabMyText.setTextColor(ContextCompat.getColor(context, R.color.tab_text));
        mTabMoreText.setTextColor(ContextCompat.getColor(context, R.color.tab_text));
    }

    private void hideFragment(FragmentTransaction transaction) {
        if (homeFragment != null) {
            transaction.hide(homeFragment);
        }
        if (financialFragment != null) {
            transaction.hide(financialFragment);
        }
        if (myFragment != null) {
            transaction.hide(myFragment);
        }
        if (moreFragment != null) {
            transaction.hide(moreFragment);
        }
    }


    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK && event.getAction() == KeyEvent.ACTION_DOWN) {
            if ((System.currentTimeMillis() - exitTime) > 2000) {
                exitTime = System.currentTimeMillis();
                AbToastUtil.showToast(this, "再按一次退出程序");
            } else {
                System.gc();
                AbActivityManager.getInstance().clearAllActivity();
            }
            return true;
        }
        return super.onKeyDown(keyCode, event);
    }


    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        switch (resultCode) {

            case 100:
                switchTab(0);

                break;

            case 110:

                switchTab(3);
                break;


            default:
        }
    }


}
